#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int tt;
    cin >> tt;
    for (int t = 0; t < tt; t++)
    {
        int m, n;
        cin >> m >> n;
        vector<int> a(m), b(n);
        for (int i = 0; i < m; i++)
            cin >> a[i];
        for (int i = 0; i < n; i++)
            cin >> b[i];
        sort(a.begin(), a.end());
        for (int i = 0; i < n; i++)// b
        {
            int minn = INT_MAX;
            int ans;
            for (int j = m-1; j >=0; j--)// a
            {
                int tmp = __builtin_popcount(b[i] ^ a[j]);
                if (tmp <= minn)
                {
                    ans = a[j];     
                    minn = tmp;               
                }
            }
            cout << ans << endl;
        }
    }
    return 0;
}